package com.cincc.cinccimdemo.ui.conversation.mention;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.lifecycle.ViewModelProvider;
import androidx.recyclerview.widget.RecyclerView;

import com.cincc.cinccimdemo.R;
import com.cincc.cinccimdemo.adapter.MentionGroupMemberAdapter;
import com.cincc.cinccimdemo.common.AttrConstants;
import com.cincc.cinccimdemo.common.ContactUtil;
import com.cincc.cinccimdemo.common.callback.OnItemClickListener;
import com.cincc.cinccimdemo.model.GroupMemberInfoUI;
import com.cincc.cinccimdemo.ui.IMBaseActivity;
import com.cincc.cinccimdemo.viewmodel.GroupMemberListViewModel;
import com.cincc.cinccimdemo.widget.SearchView;
import com.cincc.cinccimdemo.widget.SideBarView;

public class MentionGroupMemberActivity extends IMBaseActivity {

    @Override
    protected int contentLayout() {
        return R.layout.activity_group_mention;
    }

    public static final int REQUEST_PICK_MENTION_CONTACT = 1009;

    String groupId;

    RecyclerView recyclerView;
    LinearLayout descLinearLayout;
    LinearLayout emptyLinearLayout;
    SideBarView sideBarView;
    TextView mTextCenterContact;
    SearchView searchView;
    GroupMemberListViewModel memberListViewModel;

    @Override
    protected void bindViews() {
        super.bindViews();
        groupId = getIntent().getStringExtra(AttrConstants.GROUP_ID);
        recyclerView = findViewById(R.id.mList_contact);
        sideBarView = findViewById(R.id.mSide_bar_contact);
        mTextCenterContact = findViewById(R.id.mText_center_contact);
        descLinearLayout = findViewById(R.id.descLinearLayout);
        emptyLinearLayout = findViewById(R.id.emptyLinearLayout);
        searchView = findViewById(R.id.search_view);

    }

    MentionGroupMemberAdapter mentionGroupMemberAdapter;

    @Override
    protected void afterViews() {
        super.afterViews();
        mentionGroupMemberAdapter = new MentionGroupMemberAdapter(this);
        recyclerView.setAdapter(mentionGroupMemberAdapter);
        memberListViewModel = new ViewModelProvider(this).get(GroupMemberListViewModel.class);
        memberListViewModel.memberListLiveData(groupId).observe(this, friendInfos -> {
            mentionGroupMemberAdapter.setContactList(friendInfos);
            setSideView();
        });
        searchView.setOnConfirmListener(s -> {

        });
        searchView.setOnQueryTextListener(s -> {
            mentionGroupMemberAdapter.filter(s);
        });
        findViewById(R.id.cancel).setOnClickListener(v -> finish());
        mentionGroupMemberAdapter.setIOnAllMentionListener(new MentionGroupMemberAdapter.IOnAllMentionListener() {
            @Override
            public void OnClick() {
                Intent intent = new Intent();
                intent.putExtra("mentionAll", true);
                setResult(Activity.RESULT_OK, intent);
                finish();
            }
        });
        mentionGroupMemberAdapter.setOnDataClickItem(new OnItemClickListener() {
            @Override
            public void onClick(int index) {
                GroupMemberInfoUI item = mentionGroupMemberAdapter.getItem(index);
                Intent intent = new Intent();
                intent.putExtra("userId", item.memberId);
                setResult(Activity.RESULT_OK, intent);
                finish();
            }
        });
        descLinearLayout.setVisibility(View.GONE);
        emptyLinearLayout.setVisibility(View.GONE);
    }

    public void setSideView() {
        // 设置需要显示的索引栏内容
        String[] letters = ContactUtil.getLetters(1, mentionGroupMemberAdapter.getList());
        sideBarView.setLetter(letters);
        // 设置需要显示的提示框
        sideBarView.setTextView(mTextCenterContact);
        sideBarView.setOnTouchingLetterChangedListener(s -> {
            int position = ContactUtil.getLetterPosition(mentionGroupMemberAdapter.getList(), s, 1);
            if (position != -1) {
                recyclerView.scrollToPosition(position);
            }
        });
    }

    public static void startMentionActivity(Activity context, String groupId) {
        Intent i = new Intent(context, MentionGroupMemberActivity.class);
        i.putExtra(AttrConstants.GROUP_ID, groupId);
        context.startActivityForResult(i, REQUEST_PICK_MENTION_CONTACT);
    }

}
